package com.frame.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.frame.Page;
import com.frame.domain.Department;
import com.frame.support.Tree;
import com.mini.core.PageResult;
import com.mini.core.dao.MiniDao;
import com.mini.core.utils.StrKit;

/**
 * @author sxjun
 * @version 2016-6-02
 */
@Service
public class DeptService{

	@Autowired
	MiniDao miniDao;
	
	/**
	 * 查找所有菜单
	 * @return
	 */
	public List<Department> findAll(){
		String sql = "SELECT * FROM DEPARTMENT";
		return miniDao.findList(sql, Department.class);
	}
	
	/**
	 * 分页查询用户
	 * @param page
	 * @param treeId
	 * @return
	 */
	public Page<Department> paginateResult(Page<Department> page,String treeId){
		String sql = "SELECT * FROM DEPARTMENT WHERE 1=1";
		PageResult<Department> depts = null;
		if(StrKit.notBlank(treeId) && !Tree.TREE_ROOT_ID.equals(treeId)){
			sql+=" AND (PARENTID=? OR ID =?)";
			depts = miniDao.paginateResult(sql, page.getPageNo(), page.getPageSize(), Department.class,treeId,treeId);
		}else
			depts = miniDao.paginateResult(sql, page.getPageNo(), page.getPageSize(), Department.class);
		page.setPageResult(depts);
		return page;
	}

	/**
	 * 插入数据
	 * @param dept
	 */
	public void save(Department dept) {
		miniDao.insert(dept);
	}
	
	/**
	 * 更新数据
	 * @param dept
	 */
	public void update(Department dept) {
		miniDao.update(dept);
	}
	
	/**
	 * 根据Id删除菜单
	 * @param id
	 */
	public void delete(String id){
		miniDao.deleteById(Department.class, id);
	}

	/**
	 * 获取菜单
	 * @param id
	 * @return
	 */
	public Department findById(String id) {
		return miniDao.findById(Department.class, id);
	}

}
